package com.app.repository;

import com.app.configs.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Hendra on 1/4/2015.
 */
public class RPassTicketToSPV extends Repository {

    private static final Logger logger = LoggerFactory.getLogger(RPassTicketToSPV.class);

    public RPassTicketToSPV(){
        super("PassingTicketToAnother");
    }

    @Override public void post(String params){
        try {
            url = new URL(uri);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept", "application/json");
            if(params != null) {
                os = connection.getOutputStream();
                os.write(params.getBytes());
                os.flush();
            }
            if (connection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : " + connection.getResponseCode() + "Message: "
                        + connection.getResponseMessage());
            }

            br = new BufferedReader(new InputStreamReader(
                    (connection.getInputStream())));

            setMessages(br.readLine());
            connection.disconnect();

        }catch (Exception e){
            logger.error("error: ", e);
        }
    }
}
